/*
 * This file is part of the Boring Engine's Kirby Game.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package kirby;

import com.googlecode.boringengine.Graphics;
import com.googlecode.boringengine.Render;
import com.googlecode.boringengine.Sprite;
import java.util.ArrayList;
import java.util.Iterator;

public class ParticleSystem {

    private static ArrayList<Particle> particles = new ArrayList<Particle>();

    public static void update() {
        synchronized (particles) {
            Iterator<Particle> iter = particles.iterator();
            while (iter.hasNext()) {
                Particle p = iter.next();
                p.update();
                if (p.isDead)
                    iter.remove();
            }
        }
    }

    public static void draw() {
        synchronized (particles) {
            for (Particle p : particles) {
                Render.addRotation(p.rot);
                int w = p.sprite.getWidth(), h = p.sprite.getHeight();
                Graphics.drawSprite(p.sprite, (int)p.x-w/2, (int)p.y-h/2, p.scale);
                Render.addRotation(-p.rot);
            }
        }
    }

    public static void add(Particle p) {
        synchronized (particles) {
            particles.add(p);
        }
    }

    public static abstract class Particle {
        Sprite sprite;
        double x, y;
        double dx, dy, rot, drot, scale, dscale;
        boolean isDead;

        public void update() {
            x += dx; y += dy; rot += drot; scale += dscale;
            update0();
        }

        public abstract void update0();
    }
}
